package com.antstorm.statistics.util;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;

/**
 * Created by yoan on 16/3/14.
 */
public class StringUtils extends org.apache.commons.lang3.StringUtils{

    private final static char[] letters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789".toCharArray();

    public static String surroundWith(String source, String surroundedString) {
        StringBuilder sb = new StringBuilder(source);
        sb.insert(0, surroundedString);
        sb.append(surroundedString);
        return sb.toString();
    }

    /**
     * 去除最后一位字寸
     *
     * @param source
     * @return
     */
    public static String outOfLastPit(String source) {
        String result = null;
        if (source != null && !"".equals(source)){
            result = source.substring(0, source.length() - 1);
        }
        return result;
    }
    public static boolean isEmpty(CharSequence cs) {
        return cs == null || cs.length() == 0;
    }
    public static String substringBefore(String str, String separator) {
        if(!isEmpty(str) && separator != null) {
            if(separator.isEmpty()) {
                return "";
            } else {
                int pos = str.indexOf(separator);
                return pos == -1?str:str.substring(0, pos);
            }
        } else {
            return str;
        }
    }
    /**
     * 获取随机字符串
     * @param length
     * @return
     */
    public static String getRandomNumberString(int length) {
        StringBuilder builder = new StringBuilder();
        Random random = new Random();
        for(int i=0;i<length;i++){
            builder.append(letters[random.nextInt(letters.length)]);
        }
        return builder.toString();
    }

    /**
     * 组装通用订单号
     * yyyyMMddHHmmssSSS + Math.random()*1000
     * @return
     */
    public static String getRandomOrderString(){
        StringBuilder builder = new StringBuilder(new SimpleDateFormat("yyyyMMddHHmmssSSS").format(new Date()));
        builder.append((int)(Math.random() * 1000));
        return builder.toString();
    }
}
